package org.csu.newpetstoreback.controller.front;

import org.csu.newpetstoreback.common.CommonResponse;
import org.csu.newpetstoreback.service.CollectService;
import org.csu.newpetstoreback.vo.CollectVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/collect/")
public class CollectController {

    @Autowired
    private CollectService collectService;

    //获取当前用户收藏夹
    @GetMapping("collects/{username}")
    @ResponseBody
    public CommonResponse<List<CollectVO>> getCollects(@PathVariable("username")String username){
        return collectService.getCollects(username);
    }

    //添加商品到收藏夹
    @PostMapping("collects/{username}/{itemid}")
    @ResponseBody
    public CommonResponse<CollectVO> addCollect(@PathVariable("username")String username,@PathVariable("itemid")String itemId){
        // 若用户名可创建则注册用户
        return collectService.addCollect(username,itemId);
    }

    //获得具体收藏的商品信息
    @GetMapping("collects/{username}/{itemid}")
    @ResponseBody
    public CommonResponse<CollectVO> getCollectById(@PathVariable("username")String username,@PathVariable("itemid")String itemId){
        return collectService.getCollectById(username,itemId);
    }

    //判断item商品是否在收藏夹中，获得对应status
    @GetMapping("items/{username}/{itemid}")
    @ResponseBody
    public CommonResponse<CollectVO> isCollectIn(@PathVariable("username")String username,@PathVariable("itemid")String itemId){
        return collectService.isCollectIn(username,itemId);
    }

    //从收藏夹中删除商品
    @DeleteMapping("collects/{username}/{itemid}")
    @ResponseBody
    public CommonResponse<CollectVO> deleteCollectById(@PathVariable("username")String username,@PathVariable("itemid")String itemId) {
        return collectService.deleteCollectById(username,itemId);
    }

}
